﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Woddle.Core
{
    public class SerializeHelper
    {
        #region 序列化到xml   ,xml时候，定义的枚举不能为指针,以及其他的指针都不能用
        /// <summary>
        /// 序列化到xml流
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="obj"></param>
        public static void SerializeToXml(Stream stream, Object obj)
        {
            try
            {
                XmlSerializer se = new XmlSerializer(obj.GetType());
                se.Serialize(stream, obj);
            }
            catch (System.Exception ex)
            {

            }

        }

        /// <summary>
        /// 序列化到xml文件中
        /// </summary>
        /// <param name="strFileName"></param>
        /// <param name="obj"></param>
        public static void SerializeToXml(String strFileName, Object obj)
        {
            try
            {
                using (FileStream writer = new FileStream(strFileName, FileMode.Create))
                {
                    XmlSerializer se = new XmlSerializer(obj.GetType());
                    se.Serialize(writer, obj);
                }
            }
            catch (System.Exception ex)
            {

            }

        }

        /// <summary>
        /// 反序列化为对象
        /// </summary>
        /// <param name="type"></param>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static object XmlToDeserialize(Type type, Stream stream)
        {
            try
            {
                XmlSerializer xmldes = new XmlSerializer(type);
                return xmldes.Deserialize(stream);
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// 文件中的xml对象变成对象
        /// </summary>
        /// <param name="type"></param>
        /// <param name="strXmlName"></param>
        /// <returns></returns>
        public static object XmlToDeserialize(Type type, string strXmlName)
        {
            try
            {
                using (FileStream fs = new FileStream(strXmlName, FileMode.Open))
                {
                    XmlSerializer xmldes = new XmlSerializer(type);
                    return xmldes.Deserialize(fs);
                }
            }
            catch (Exception e)
            {

                return null;
            }
        }


        #endregion

        #region 序列化到二进制文件

        /// <summary>
        /// 序列化到xml流
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="obj"></param>
        public static void SerializeToBinary(Stream stream, Object obj)
        {
            IFormatter fomat = new BinaryFormatter();
            fomat.Serialize(stream, obj);
        }

        /// <summary>
        /// 序列化到xml文件中
        /// </summary>
        /// <param name="strFileName"></param>
        /// <param name="obj"></param>
        public static void SerializeToBinary(String strFileName, Object obj)
        {
            try
            {
                using (FileStream writer = new FileStream(strFileName, FileMode.Create))
                {
                    IFormatter fomat = new BinaryFormatter();
                    fomat.Serialize(writer, obj);
                }
            }
            catch (System.Exception ex)
            {
                String strErrMessage = ex.Message.ToString();
                return;
            }

        }

        /// <summary>
        /// 反序列化为对象
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static object BinaryToDeserialize(Stream stream)
        {
            IFormatter fomat = new BinaryFormatter();
            return fomat.Deserialize(stream);
        }

        /// <summary>
        /// 文件中的xml对象变成对象
        /// </summary>
        /// <param name="strXmlName"></param>
        /// <returns></returns>
        public static object BinaryToDeserialize(string strXmlName)
        {
            using (FileStream fs = new FileStream(strXmlName, FileMode.Open))
            {
                IFormatter fomat = new BinaryFormatter();
                return fomat.Deserialize(fs);
            }
        }

        #endregion
    }
}
